import {Component, ViewChild, ChangeDetectorRef} from '@angular/core';
import {
  NavController,
  Slides,
  Content,
  ActionSheetController,
  PopoverController, NavParams
} from 'ionic-angular';
import {PayPage} from '../pay/pay';
import {Helper} from "../../common/Helper";
import {ModalShareComponent} from "../../components/modal-share/modal-share";
import {AppShare} from '../../common/share';
import {ProductProvider} from "../../providers/product/product";
import {LoginPage} from "../login/login";

@Component({
  selector: 'page-product-detail',
  templateUrl: 'product-detail.html',
})
export class ProductDetailPage {

  payPage: any

  @ViewChild('slides') slides: Slides
  @ViewChild('gDefaultBox') gDefaultBox: any
  @ViewChild('gDefault') gDefault: any

  @ViewChild(Content) content: Content

  segment: number = 0
  fixed: boolean = false
  recordList: object[] = []
  detail: any = {}

  constructor(private cdRef: ChangeDetectorRef,
              public helper: Helper,
              public actionSheetCtrl: ActionSheetController,
              public popoverCtrl: PopoverController,
              public share: AppShare,
              public productProvider: ProductProvider,
              public navCtrl: NavController,
              public navParams: NavParams) {
    this.payPage = PayPage
  }

  getDetail() {
    this.productProvider.getDetail({
      loading: false,
      data: {
        id: this.navParams.get('id')
      },
      success: (d) => {
        this.detail = d ? d : {}
      }
    })
  }

  getRecordList() {
    this.productProvider.getRecordList({
      data: {
        id: this.navParams.get('id')
      },
      success: (d) => {
        this.recordList = d.list
      }
    })
  }

  gotoPage(item) {
    const config = {
      'pay': PayPage
    }
    this.helper.checkLogin(() => {
      if (item == 'pay' && !this.canBid()) return
      this.navCtrl.push(config[item], {
        id: this.navParams.get('id')
      })
    }, () => {
      this.navCtrl.push(LoginPage)
    })
  }

  canBid() {
    return !(Number(this.detail.status) > 10)
  }

  openShareModal() {
    let buttons = [];
    if (this.share.wechatInstalled) {
      buttons = buttons.concat([
        {
          text: '微信好友',
          icon: 'weixin-friend',
          handler: () => {
            this.share.wxShare1()
          }
        },
        {
          text: '朋友圈',
          icon: 'weixin',
          handler: () => {
            this.share.wxShare(1)
          }
        },
        {
          text: 'QQ好友',
          icon: 'qq',
          handler: () => {
            this.share.wxShare(1)
          }
        },
        {
          text: '新浪微博',
          icon: 'weibo',
          handler: () => {
            this.share.wxShare(1)
          }
        }
      ])
    }

    buttons.push({
      text: '取消',
      role: 'cancel'
    })

    let actionSheet = this.actionSheetCtrl.create({
      cssClass: 'g-modal-share',
      buttons: buttons
    });
    actionSheet.present();
  }

  openShare(myEvent) {
    let popover = this.popoverCtrl.create(ModalShareComponent, {}, {
      cssClass: 'g-modal-share'
    });
    popover.present({
      ev: myEvent
    });
  }

  segmentSelect() {
    setTimeout(() => {
      this.contentScroll()
    }, 500)
  }

  contentScroll() {
    let top = this.content.scrollTop
    if (top > 330) return
    if (this.gDefault) {
      this.gDefault.refreshSize()
    }
    if (top >= 230) {
      this.fixed = true
    } else {
      this.fixed = false
    }
    //从该组件到各个子组件执行一次变化检测
    this.cdRef.detectChanges()
  }

  ionViewWillEnter() {
    Helper.setStatusBarTranslucence()
    this.contentScroll()
    this.getDetail()
    this.getRecordList()
  }

  ionViewWillLeave() {
    this.fixed = false
  }
}
